package com.zx.zxapibackend.security.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.zxapibackend.mapper.UserMapper;
import com.zx.zxapibackend.model.domain.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author 张旭
 * @version 1.0
 */

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("userAccount", username);
        userQueryWrapper.or().eq("mobile", username);


        User user = userMapper.selectOne(userQueryWrapper);

        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        System.out.println("走了自定义userDetails认证源。。。。。");

        //隐藏手机号
        String mobile = user.getMobile();
        String newPhoneNum = mobile.substring(0, 3) + "****" + mobile.substring(7);
        user.setMobile(newPhoneNum);
        return user;

    }
}
